Mapping with R

Welcome to Course

Welcome to Mapping with R

Why making maps with R makes sense

R is a complete GIS tool

Two types of data: vector and raster. We’ll mostly cover vector.

Make simple maps with ggplot and mapview

Access Data

library(rnaturalearth)

ne_countries()
Simple feature collection with 177 features and 168 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
Geodetic CRS:  WGS 84
First 10 features:
        featurecla scalerank labelrank                  sovereignt sov_a3
1  Admin-0 country         1         6                        Fiji    FJI
2  Admin-0 country         1         3 United Republic of Tanzania    TZA
3  Admin-0 country         1         7              Western Sahara    SAH
4  Admin-0 country         1         2                      Canada    CAN
5  Admin-0 country         1         2    United States of America    US1
6  Admin-0 country         1         3                  Kazakhstan    KA1
7  Admin-0 country         1         3                  Uzbekistan    UZB
8  Admin-0 country         1         2            Papua New Guinea    PNG
9  Admin-0 country         1         2                   Indonesia    IDN
10 Admin-0 country         1         2                   Argentina    ARG
   adm0_dif level              type tlc                       admin adm0_a3
1         0     2 Sovereign country   1                        Fiji     FJI
2         0     2 Sovereign country   1 United Republic of Tanzania     TZA
3         0     2     Indeterminate   1              Western Sahara     SAH
4         0     2 Sovereign country   1                      Canada     CAN
5         1     2           Country   1    United States of America     USA
6         1     1       Sovereignty   1                  Kazakhstan     KAZ
7         0     2 Sovereign country   1                  Uzbekistan     UZB
8         0     2 Sovereign country   1            Papua New Guinea     PNG
9         0     2 Sovereign country   1                   Indonesia     IDN
10        0     2 Sovereign country   1                   Argentina     ARG
   geou_dif                  geounit gu_a3 su_dif          subunit su_a3
1         0                     Fiji   FJI      0             Fiji   FJI
2         0                 Tanzania   TZA      0         Tanzania   TZA
3         0           Western Sahara   SAH      0   Western Sahara   SAH
4         0                   Canada   CAN      0           Canada   CAN
5         0 United States of America   USA      0    United States   USA
6         0               Kazakhstan   KAZ      0       Kazakhstan   KAZ
7         0               Uzbekistan   UZB      0       Uzbekistan   UZB
8         0         Papua New Guinea   PNG      1 Papua New Guinea   PN1
9         0                Indonesia   IDN      0        Indonesia   IDN
10        0                Argentina   ARG      0        Argentina   ARG
   brk_diff                     name        name_long brk_a3         brk_name
1         0                     Fiji             Fiji    FJI             Fiji
2         0                 Tanzania         Tanzania    TZA         Tanzania
3         1                W. Sahara   Western Sahara    B28        W. Sahara
4         0                   Canada           Canada    CAN           Canada
5         0 United States of America    United States    USA    United States
6         0               Kazakhstan       Kazakhstan    KAZ       Kazakhstan
7         0               Uzbekistan       Uzbekistan    UZB       Uzbekistan
8         0         Papua New Guinea Papua New Guinea    PN1 Papua New Guinea
9         0                Indonesia        Indonesia    IDN        Indonesia
10        0                Argentina        Argentina    ARG        Argentina
   brk_group  abbrev postal                             formal_en formal_fr
1       <NA>    Fiji     FJ                      Republic of Fiji      <NA>
2       <NA>   Tanz.     TZ           United Republic of Tanzania      <NA>
3       <NA> W. Sah.     WS      Sahrawi Arab Democratic Republic      <NA>
4       <NA>    Can.     CA                                Canada      <NA>
5       <NA>  U.S.A.     US              United States of America      <NA>
6       <NA>    Kaz.     KZ                Republic of Kazakhstan      <NA>
7       <NA>    Uzb.     UZ                Republic of Uzbekistan      <NA>
8       <NA>  P.N.G.     PG Independent State of Papua New Guinea      <NA>
9       <NA>   Indo.   INDO                 Republic of Indonesia      <NA>
10      <NA>    Arg.     AR                    Argentine Republic      <NA>
         name_ciawf note_adm0                        note_brk
1              Fiji      <NA>                            <NA>
2          Tanzania      <NA>                            <NA>
3    Western Sahara      <NA> Self admin.; Claimed by Morocco
4            Canada      <NA>                            <NA>
5     United States      <NA>                            <NA>
6        Kazakhstan      <NA>                            <NA>
7        Uzbekistan      <NA>                            <NA>
8  Papua New Guinea      <NA>                            <NA>
9         Indonesia      <NA>                            <NA>
10        Argentina      <NA>                            <NA>
                  name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13
1                      Fiji     <NA>         5         1         2          2
2                  Tanzania     <NA>         3         6         2          2
3            Western Sahara     <NA>         4         7         4          4
4                    Canada     <NA>         6         6         2          2
5  United States of America     <NA>         4         5         1          1
6                Kazakhstan     <NA>         6         1         6          1
7                Uzbekistan     <NA>         2         3         5          4
8          Papua New Guinea     <NA>         4         2         3          1
9                 Indonesia     <NA>         6         6         6         11
10                Argentina     <NA>         3         1         3         13
     pop_est pop_rank pop_year   gdp_md gdp_year                   economy
1     889953       11     2019     5496     2019      6. Developing region
2   58005463       16     2019    63177     2019 7. Least developed region
3     603253       11     2017      907     2007 7. Least developed region
4   37589262       15     2019  1736425     2019   1. Developed region: G7
5  328239523       17     2019 21433226     2019   1. Developed region: G7
6   18513930       14     2019   181665     2019      6. Developing region
7   33580650       15     2019    57921     2019      6. Developing region
8    8776109       13     2019    24829     2019      6. Developing region
9  270625568       17     2019  1119190     2019  4. Emerging region: MIKT
10  44938712       15     2019   445445     2019   5. Emerging region: G20
               income_grp fips_10 iso_a2 iso_a2_eh iso_a3 iso_a3_eh iso_n3
1  4. Lower middle income      FJ     FJ        FJ    FJI       FJI    242
2           5. Low income      TZ     TZ        TZ    TZA       TZA    834
3           5. Low income      WI     EH        EH    ESH       ESH    732
4    1. High income: OECD      CA     CA        CA    CAN       CAN    124
5    1. High income: OECD      US     US        US    USA       USA    840
6  3. Upper middle income      KZ     KZ        KZ    KAZ       KAZ    398
7  4. Lower middle income      UZ     UZ        UZ    UZB       UZB    860
8  4. Lower middle income      PP     PG        PG    PNG       PNG    598
9  4. Lower middle income      ID     ID        ID    IDN       IDN    360
10 3. Upper middle income      AR     AR        AR    ARG       ARG    032
   iso_n3_eh un_a3 wb_a2 wb_a3   woe_id woe_id_eh
1        242   242    FJ   FJI 23424813  23424813
2        834   834    TZ   TZA 23424973  23424973
3        732   732   -99   -99 23424990  23424990
4        124   124    CA   CAN 23424775  23424775
5        840   840    US   USA 23424977  23424977
6        398   398    KZ   KAZ      -90  23424871
7        860   860    UZ   UZB 23424980  23424980
8        598   598    PG   PNG 23424926  23424926
9        360   360    ID   IDN 23424846  23424846
10       032   032    AR   ARG 23424747  23424747
                                                      woe_note adm0_iso
1                                   Exact WOE match as country      FJI
2                                   Exact WOE match as country      TZA
3                                   Exact WOE match as country      B28
4                                   Exact WOE match as country      CAN
5                                   Exact WOE match as country      USA
6  Includes Baykonur Cosmodrome as an Admin-1 states provinces      KAZ
7                                   Exact WOE match as country      UZB
8                                   Exact WOE match as country      PN1
9                                   Exact WOE match as country      IDN
10                                  Exact WOE match as country      ARG
   adm0_diff adm0_tlc adm0_a3_us adm0_a3_fr adm0_a3_ru adm0_a3_es adm0_a3_cn
1       <NA>      FJI        FJI        FJI        FJI        FJI        FJI
2       <NA>      TZA        TZA        TZA        TZA        TZA        TZA
3       <NA>      B28        SAH        MAR        SAH        SAH        SAH
4       <NA>      CAN        CAN        CAN        CAN        CAN        CAN
5       <NA>      USA        USA        USA        USA        USA        USA
6       <NA>      KAZ        KAZ        KAZ        KAZ        KAZ        KAZ
7       <NA>      UZB        UZB        UZB        UZB        UZB        UZB
8       <NA>      PN1        PNG        PNG        PNG        PNG        PNG
9       <NA>      IDN        IDN        IDN        IDN        IDN        IDN
10      <NA>      ARG        ARG        ARG        ARG        ARG        ARG
   adm0_a3_tw adm0_a3_in adm0_a3_np adm0_a3_pk adm0_a3_de adm0_a3_gb adm0_a3_br
1         FJI        FJI        FJI        FJI        FJI        FJI        FJI
2         TZA        TZA        TZA        TZA        TZA        TZA        TZA
3         SAH        MAR        SAH        SAH        SAH        SAH        SAH
4         CAN        CAN        CAN        CAN        CAN        CAN        CAN
5         USA        USA        USA        USA        USA        USA        USA
6         KAZ        KAZ        KAZ        KAZ        KAZ        KAZ        KAZ
7         UZB        UZB        UZB        UZB        UZB        UZB        UZB
8         PNG        PNG        PNG        PNG        PNG        PNG        PNG
9         IDN        IDN        IDN        IDN        IDN        IDN        IDN
10        ARG        ARG        ARG        ARG        ARG        ARG        ARG
   adm0_a3_il adm0_a3_ps adm0_a3_sa adm0_a3_eg adm0_a3_ma adm0_a3_pt adm0_a3_ar
1         FJI        FJI        FJI        FJI        FJI        FJI        FJI
2         TZA        TZA        TZA        TZA        TZA        TZA        TZA
3         SAH        MAR        MAR        SAH        MAR        SAH        SAH
4         CAN        CAN        CAN        CAN        CAN        CAN        CAN
5         USA        USA        USA        USA        USA        USA        USA
6         KAZ        KAZ        KAZ        KAZ        KAZ        KAZ        KAZ
7         UZB        UZB        UZB        UZB        UZB        UZB        UZB
8         PNG        PNG        PNG        PNG        PNG        PNG        PNG
9         IDN        IDN        IDN        IDN        IDN        IDN        IDN
10        ARG        ARG        ARG        ARG        ARG        ARG        ARG
   adm0_a3_jp adm0_a3_ko adm0_a3_vn adm0_a3_tr adm0_a3_id adm0_a3_pl adm0_a3_gr
1         FJI        FJI        FJI        FJI        FJI        FJI        FJI
2         TZA        TZA        TZA        TZA        TZA        TZA        TZA
3         SAH        SAH        SAH        MAR        MAR        MAR        SAH
4         CAN        CAN        CAN        CAN        CAN        CAN        CAN
5         USA        USA        USA        USA        USA        USA        USA
6         KAZ        KAZ        KAZ        KAZ        KAZ        KAZ        KAZ
7         UZB        UZB        UZB        UZB        UZB        UZB        UZB
8         PNG        PNG        PNG        PNG        PNG        PNG        PNG
9         IDN        IDN        IDN        IDN        IDN        IDN        IDN
10        ARG        ARG        ARG        ARG        ARG        ARG        ARG
   adm0_a3_it adm0_a3_nl adm0_a3_se adm0_a3_bd adm0_a3_ua adm0_a3_un adm0_a3_wb
1         FJI        FJI        FJI        FJI        FJI        -99        -99
2         TZA        TZA        TZA        TZA        TZA        -99        -99
3         SAH        MAR        SAH        SAH        SAH        -99        -99
4         CAN        CAN        CAN        CAN        CAN        -99        -99
5         USA        USA        USA        USA        USA        -99        -99
6         KAZ        KAZ        KAZ        KAZ        KAZ        -99        -99
7         UZB        UZB        UZB        UZB        UZB        -99        -99
8         PNG        PNG        PNG        PNG        PNG        -99        -99
9         IDN        IDN        IDN        IDN        IDN        -99        -99
10        ARG        ARG        ARG        ARG        ARG        -99        -99
       continent region_un          subregion                  region_wb
1        Oceania   Oceania          Melanesia        East Asia & Pacific
2         Africa    Africa     Eastern Africa         Sub-Saharan Africa
3         Africa    Africa    Northern Africa Middle East & North Africa
4  North America  Americas   Northern America              North America
5  North America  Americas   Northern America              North America
6           Asia      Asia       Central Asia      Europe & Central Asia
7           Asia      Asia       Central Asia      Europe & Central Asia
8        Oceania   Oceania          Melanesia        East Asia & Pacific
9           Asia      Asia South-Eastern Asia        East Asia & Pacific
10 South America  Americas      South America  Latin America & Caribbean
   name_len long_len abbrev_len tiny homepart min_zoom min_label max_label
1         4        4          4  -99        1      0.0       3.0       8.0
2         8        8          5  -99        1      0.0       3.0       8.0
3         9       14          7  -99        1      4.7       6.0      11.0
4         6        6          4  -99        1      0.0       1.7       5.7
5        24       13          6  -99        1      0.0       1.7       5.7
6        10       10          4  -99        1      0.0       2.7       7.0
7        10       10          4    5        1      0.0       3.0       8.0
8        16       16          6  -99        1      0.0       2.5       7.5
9         9        9          5  -99        1      0.0       1.7       6.7
10        9        9          4  -99        1      0.0       2.0       7.0
      label_x    label_y      ne_id wikidataid             name_ar
1   177.97543 -17.826099 1159320625       Q712                فيجي
2    34.95918  -6.051866 1159321337       Q924             تنزانيا
3   -12.63030  23.967592 1159321223      Q6250     الصحراء الغربية
4  -101.91070  60.324287 1159320467        Q16                كندا
5   -97.48260  39.538479 1159321369        Q30    الولايات المتحدة
6    68.68555  49.054149 1159320967       Q232           كازاخستان
7    64.00543  41.693603 1159321405       Q265           أوزبكستان
8   143.91022  -5.695285 1159321173       Q691 بابوا غينيا الجديدة
9   101.89295  -0.954404 1159320845       Q252           إندونيسيا
10  -64.17333 -33.501159 1159320331       Q414           الأرجنتين
           name_bn            name_de                  name_en
1             ফিজি            Fidschi                     Fiji
2        তানজানিয়া           Tansania                 Tanzania
3     পশ্চিম সাহারা         Westsahara           Western Sahara
4           কানাডা             Kanada                   Canada
5  মার্কিন যুক্তরাষ্ট্র Vereinigte Staaten United States of America
6        কাজাখস্তান         Kasachstan               Kazakhstan
7       উজবেকিস্তান         Usbekistan               Uzbekistan
8    পাপুয়া নিউগিনি    Papua-Neuguinea         Papua New Guinea
9       ইন্দোনেশিয়া         Indonesien                Indonesia
10       আর্জেন্টিনা        Argentinien                Argentina
              name_es             name_fa                   name_fr
1                Fiyi                فیجی                     Fidji
2            Tanzania            تانزانیا                  Tanzanie
3   Sahara Occidental          صحرای غربی         Sahara occidental
4              Canadá              کانادا                    Canada
5      Estados Unidos ایالات متحده آمریکا                États-Unis
6          Kazajistán            قزاقستان                Kazakhstan
7          Uzbekistán            ازبکستان               Ouzbékistan
8  Papúa Nueva Guinea       پاپوآ گینه نو Papouasie-Nouvelle-Guinée
9           Indonesia             اندونزی                 Indonésie
10          Argentina            آرژانتین                 Argentine
                       name_el           name_he          name_hi
1                        Φίτζι             פיג'י             फ़िजी
2                     Τανζανία            טנזניה          तंज़ानिया
3                Δυτική Σαχάρα      סהרה המערבית     पश्चिमी सहारा
4                      Καναδάς              קנדה            कनाडा
5  Ηνωμένες Πολιτείες Αμερικής       ארצות הברית संयुक्त राज्य अमेरिका
6                    Καζακστάν            קזחסטן        कज़ाख़िस्तान
7                 Ουζμπεκιστάν         אוזבקיסטן        उज़्बेकिस्तान
8           Παπούα Νέα Γουινέα פפואה גינאה החדשה     पापुआ न्यू गिनी
9                    Ινδονησία         אינדונזיה         इंडोनेशिया
10                   Αργεντινή          ארגנטינה         अर्जेण्टीना
                     name_hu         name_id               name_it
1            Fidzsi-szigetek            Fiji                  Figi
2                   Tanzánia        Tanzania              Tanzania
3             Nyugat-Szahara    Sahara Barat    Sahara Occidentale
4                     Kanada          Kanada                Canada
5  Amerikai Egyesült Államok Amerika Serikat Stati Uniti d'America
6                 Kazahsztán      Kazakhstan            Kazakistan
7                Üzbegisztán      Uzbekistan            Uzbekistan
8            Pápua Új-Guinea    Papua Nugini    Papua Nuova Guinea
9                  Indonézia       Indonesia             Indonesia
10                 Argentína       Argentina             Argentina
              name_ja      name_ko                      name_nl
1            フィジー         피지                         Fiji
2          タンザニア     탄자니아                     Tanzania
3            西サハラ     서사하라            Westelijke Sahara
4              カナダ       캐나다                       Canada
5      アメリカ合衆国         미국 Verenigde Staten van Amerika
6        カザフスタン   카자흐스탄                   Kazachstan
7      ウズベキスタン 우즈베키스탄                  Oezbekistan
8  パプアニューギニア 파푸아뉴기니          Papoea-Nieuw-Guinea
9        インドネシア   인도네시아                    Indonesië
10       アルゼンチン   아르헨티나                   Argentinië
             name_pl          name_pt              name_ru          name_sv
1              Fidżi             Fiji                Фиджи             Fiji
2           Tanzania         Tanzânia             Танзания         Tanzania
3   Sahara Zachodnia   Sara Ocidental      Западная Сахара       Västsahara
4             Kanada           Canadá               Канада           Kanada
5  Stany Zjednoczone   Estados Unidos                  США              USA
6         Kazachstan      Cazaquistão            Казахстан        Kazakstan
7         Uzbekistan      Uzbequistão           Узбекистан       Uzbekistan
8  Papua-Nowa Gwinea Papua-Nova Guiné Папуа — Новая Гвинея Papua Nya Guinea
9          Indonezja        Indonésia            Индонезия       Indonesien
10         Argentyna        Argentina            Аргентина        Argentina
                       name_tr                 name_uk                name_ur
1                         Fiji                   Фіджі                    فجی
2                     Tanzanya                Танзанія                تنزانیہ
3                   Batı Sahra          Західна Сахара            مغربی صحارا
4                       Kanada                  Канада                 کینیڈا
5  Amerika Birleşik Devletleri Сполучені Штати Америки ریاستہائے متحدہ امریکا
6                   Kazakistan               Казахстан               قازقستان
7                   Özbekistan              Узбекистан               ازبکستان
8              Papua Yeni Gine       Папуа Нова Гвінея          پاپوا نیو گنی
9                    Endonezya               Індонезія              انڈونیشیا
10                    Arjantin               Аргентина               ارجنٹائن
            name_vi        name_zh       name_zht         fclass_iso tlc_diff
1              Fiji           斐济           斐濟    Admin-0 country     <NA>
2          Tanzania       坦桑尼亚       坦尚尼亞    Admin-0 country     <NA>
3        Tây Sahara       西撒哈拉       西撒哈拉 Admin-0 dependency     <NA>
4            Canada         加拿大         加拿大    Admin-0 country     <NA>
5            Hoa Kỳ           美国           美國    Admin-0 country     <NA>
6        Kazakhstan     哈萨克斯坦         哈薩克    Admin-0 country     <NA>
7        Uzbekistan   乌兹别克斯坦       烏茲別克    Admin-0 country     <NA>
8  Papua New Guinea 巴布亚新几内亚 巴布亞紐幾內亞    Admin-0 country     <NA>
9         Indonesia     印度尼西亚     印度尼西亞    Admin-0 country     <NA>
10        Argentina         阿根廷         阿根廷    Admin-0 country     <NA>
           fclass_tlc fclass_us    fclass_fr fclass_ru fclass_es fclass_cn
1     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
2     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
3  Admin-0 dependency      <NA> Unrecognized      <NA>      <NA>      <NA>
4     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
5     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
6     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
7     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
8     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
9     Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
10    Admin-0 country      <NA>         <NA>      <NA>      <NA>      <NA>
   fclass_tw    fclass_in fclass_np fclass_pk fclass_de fclass_gb fclass_br
1       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
2       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
3       <NA> Unrecognized      <NA>      <NA>      <NA>      <NA>      <NA>
4       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
5       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
6       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
7       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
8       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
9       <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
10      <NA>         <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
   fclass_il    fclass_ps    fclass_sa fclass_eg    fclass_ma fclass_pt
1       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
2       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
3       <NA> Unrecognized Unrecognized      <NA> Unrecognized      <NA>
4       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
5       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
6       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
7       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
8       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
9       <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
10      <NA>         <NA>         <NA>      <NA>         <NA>      <NA>
   fclass_ar fclass_jp fclass_ko fclass_vn    fclass_tr    fclass_id
1       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
2       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
3       <NA>      <NA>      <NA>      <NA> Unrecognized Unrecognized
4       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
5       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
6       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
7       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
8       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
9       <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
10      <NA>      <NA>      <NA>      <NA>         <NA>         <NA>
      fclass_pl fclass_gr fclass_it    fclass_nl fclass_se fclass_bd fclass_ua
1          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
2          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
3  Unrecognized      <NA>      <NA> Unrecognized      <NA>      <NA>      <NA>
4          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
5          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
6          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
7          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
8          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
9          <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
10         <NA>      <NA>      <NA>         <NA>      <NA>      <NA>      <NA>
                         geometry
1  MULTIPOLYGON (((180 -16.067...
2  MULTIPOLYGON (((33.90371 -0...
3  MULTIPOLYGON (((-8.66559 27...
4  MULTIPOLYGON (((-122.84 49,...
5  MULTIPOLYGON (((-122.84 49,...
6  MULTIPOLYGON (((87.35997 49...
7  MULTIPOLYGON (((55.96819 41...
8  MULTIPOLYGON (((141.0002 -2...
9  MULTIPOLYGON (((141.0002 -2...
10 MULTIPOLYGON (((-68.63401 -...

Make simple map with ggplot

library(tidyverse)

ne_countries() |>
  ggplot() +
  geom_sf()

Make simple map with mapview

library(mapview)

ne_countries() |>
  mapview()

How course works

Geospatial Data: Vector Data

Simple Features Data

Always use sf data

source

Learn More

https://r-spatial.github.io/sf/articles/sf1.html

Geometry Type

POLYGON

library(sf)

wyoming <-
  read_sf("data/wyoming.geojson")

wyoming
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -111.0546 ymin: 40.99477 xmax: -104.0522 ymax: 45.00582
Geodetic CRS:  WGS 84
# A tibble: 1 × 2
  NAME                                                                  geometry
  <chr>                                                            <POLYGON [°]>
1 Wyoming ((-106.3212 40.99912, -106.3262 40.99927, -106.3265 40.99927, -106.33…
ggplot() +
  geom_sf(data = wyoming)

POINT

wyoming_one_ev_station <-
  read_sf("data/wyoming-one-ev-station.geojson")

wyoming_one_ev_station
Simple feature collection with 1 feature and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -104.8373 ymin: 41.13005 xmax: -104.8373 ymax: 41.13005
Geodetic CRS:  WGS 84
# A tibble: 1 × 2
  objectid             geometry
     <dbl>          <POINT [°]>
1     6703 (-104.8373 41.13005)
ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_one_ev_station)

LINESTRING

wyoming_highway_30 <-
  read_sf("data/wyoming-highway-30.geojson")

wyoming_highway_30
Simple feature collection with 1 feature and 4 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -107.5054 ymin: 41.74157 xmax: -106.8305 ymax: 41.78956
Geodetic CRS:  NAD83
# A tibble: 1 × 5
  linearid       fullname  rttyp mtfcc                                  geometry
  <chr>          <chr>     <chr> <chr>                          <LINESTRING [°]>
1 11010932011560 US Hwy 30 U     S1100 (-106.8305 41.74157, -106.8314 41.74161,…
ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_highway_30)

MULTIPOLYGON

rhode_island <-
  read_sf("data/rhode-island.geojson")

rhode_island
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -71.89239 ymin: 41.14656 xmax: -71.12052 ymax: 42.01888
Geodetic CRS:  WGS 84
# A tibble: 1 × 1
                                                                        geometry
                                                              <MULTIPOLYGON [°]>
1 (((-71.53173 41.3792, -71.53177 41.37915, -71.53182 41.37915, -71.53187 41.37…
ggplot() +
  geom_sf(data = rhode_island)

MULTIPOINT

wyoming_all_ev_stations <-
  read_sf("data/wyoming-all-ev-stations.geojson")

wyoming_all_ev_stations
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOINT
Dimension:     XY
Bounding box:  xmin: -110.9853 ymin: 41.12704 xmax: -104.4527 ymax: 44.97646
Geodetic CRS:  WGS 84
# A tibble: 1 × 1
                                                                        geometry
                                                                <MULTIPOINT [°]>
1 ((-104.8373 41.13005), (-110.7649 43.4779), (-104.8406 41.12704), (-104.8406 …
ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_all_ev_stations)

MULTILINESTRING

wyoming_roads <-
  read_sf("data/wyoming-roads.geojson")

wyoming_roads
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: -111.0545 ymin: 40.9972 xmax: -104.0525 ymax: 45.00457
Geodetic CRS:  NAD83
# A tibble: 1 × 1
                                                                        geometry
                                                           <MULTILINESTRING [°]>
1 ((-106.4019 42.85946, -106.402 42.85933, -106.4036 42.85766, -106.4039 42.857…
ggplot() +
  geom_sf(data = wyoming) +
  geom_sf(data = wyoming_roads)

Dimensions

wyoming
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -111.0546 ymin: 40.99477 xmax: -104.0522 ymax: 45.00582
Geodetic CRS:  WGS 84
# A tibble: 1 × 2
  NAME                                                                  geometry
  <chr>                                                            <POLYGON [°]>
1 Wyoming ((-106.3212 40.99912, -106.3262 40.99927, -106.3265 40.99927, -106.33…

Bounding Box

rhode_island
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -71.89239 ymin: 41.14656 xmax: -71.12052 ymax: 42.01888
Geodetic CRS:  WGS 84
# A tibble: 1 × 1
                                                                        geometry
                                                              <MULTIPOLYGON [°]>
1 (((-71.53173 41.3792, -71.53177 41.37915, -71.53182 41.37915, -71.53187 41.37…
rhode_island_bounding_box
Simple feature collection with 1 feature and 0 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -71.89239 ymin: 41.14656 xmax: -71.12052 ymax: 42.01888
Geodetic CRS:  WGS 84
                        geometry
1 POLYGON ((-71.89239 41.1465...
ggplot() +
  geom_sf(data = rhode_island) +
  geom_sf(
    data = rhode_island_bounding_box,
    color = "red",
    linewidth = 1,
    fill = NA
  )

Coordinate Reference System (CRS)

wyoming
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -111.0546 ymin: 40.99477 xmax: -104.0522 ymax: 45.00582
Geodetic CRS:  WGS 84
# A tibble: 1 × 2
  NAME                                                                  geometry
  <chr>                                                            <POLYGON [°]>
1 Wyoming ((-106.3212 40.99912, -106.3262 40.99927, -106.3265 40.99927, -106.33…
wyoming_different_projection
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -1250364 ymin: 2027604 xmax: -633753.5 ymax: 2539525
Projected CRS: NAD83 / Conus Albers
# A tibble: 1 × 2
  NAME                                                                  geometry
* <chr>                                                            <POLYGON [m]>
1 Wyoming ((-859557.4 2045615, -859974.4 2045677, -859994.4 2045680, -860968.8 …
wyoming |>
  ggplot() +
  geom_sf()

wyoming_different_projection |>
  ggplot() +
  geom_sf()

geometry Column

wyoming
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -111.0546 ymin: 40.99477 xmax: -104.0522 ymax: 45.00582
Geodetic CRS:  WGS 84
# A tibble: 1 × 2
  NAME                                                                  geometry
  <chr>                                                            <POLYGON [°]>
1 Wyoming ((-106.3212 40.99912, -106.3262 40.99927, -106.3265 40.99927, -106.33…
wyoming_coordinates
# A tibble: 8,583 × 2
   longitude latitude
       <dbl>    <dbl>
 1 -106.3212 40.99912
 2 -106.3262 40.99927
 3 -106.3265 40.99927
 4 -106.3382 40.99953
 5 -106.3414 40.99957
 6 -106.3464 40.99964
 7 -106.3514 40.99997
 8 -106.3540 40.99997
 9 -106.3588 41.00014
10 -106.3756 41.00072
# ℹ 8,573 more rows
wyoming_coordinates |>
  ggplot(aes(
    x = longitude,
    y = latitude
  )) +
  geom_point()

Your Turn

  • Run the following code in order to import an sf object called africa.
library(tidyverse)

africa <-
  read_rds("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/africa.rds")

Your Turn

  • Examine your object, ensuring you can identify its geometry type, dimensions, bounding box, coordinate reference system and geometry column.

  • If you want to try making a static with the geom_sf() function from ggplot and/or an interactive map with the mapview() function from the {mapview} package.

Learn More

https://r-spatial.github.io/sf/articles/sf1.html

Accessing Raw Geospatial Data

{sf} Package

Geospatial Data Formats: ESRI Shapefile

ESRI Shapefile

Consists of multiple files:

  1. .shp: The main file that contains the geometry data (shapes) of the features.
  2. .shx: The shape index file, which stores the positional index of the feature geometry to improve access speed.
  3. .dbf: The attribute format file, which stores attribute data in a tabular format. This file uses the dBase format.

ESRI Shapefile

In addition to these three mandatory files, there are several optional files that can accompany a shapefile:

  • .prj: The projection file, which contains coordinate system and projection information.
  • .sbn and .sbx: Spatial index files that improve the speed of spatial queries.
  • .fbn and .fbx: Files used for spatial indexing of features that have been deleted.

ESRI Shapefile

  • .ain and .aih: Attribute index files that improve the speed of attribute queries.
  • .ixs and .mxs: Geocoding index for read-write and read-only datasets, respectively.
  • .cpg: The code page file, which specifies the character encoding used in the .dbf file.

ESRI Shapefile

read_sf("data/Portland_City_Council_Districts.shp")
Simple feature collection with 4 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -13674130 ymin: 5689875 xmax: -13633520 ymax: 5724898
Projected CRS: WGS 84 / Pseudo-Mercator
# A tibble: 4 × 10
  OBJECTID DISTRICT pop_total Ideal_pop Diff_Ideal DDeviation ODeviation
     <int> <chr>        <int>     <dbl>      <int>      <dbl>      <dbl>
1        1 1           159498  163125.8      -3628 -2.223898    4.992467
2        2 2           160716  163125.8      -2410 -1.477235    4.992467
3        3 3           167642  163125.8       4516  2.768570    4.992467
4        4 4           164647  163125.8       1521  0.9325628   4.992467
# ℹ 3 more variables: Shape_Leng <dbl>, Shape_Area <dbl>,
#   geometry <MULTIPOLYGON [m]>

TODO: center slide

read_sf("data/Portland_City_Council_Districts.shp")
Simple feature collection with 4 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -13674130 ymin: 5689875 xmax: -13633520 ymax: 5724898
Projected CRS: WGS 84 / Pseudo-Mercator
# A tibble: 4 × 10
  OBJECTID DISTRICT pop_total Ideal_pop Diff_Ideal DDeviation ODeviation
     <int> <chr>        <int>     <dbl>      <int>      <dbl>      <dbl>
1        1 1           159498  163125.8      -3628 -2.223898    4.992467
2        2 2           160716  163125.8      -2410 -1.477235    4.992467
3        3 3           167642  163125.8       4516  2.768570    4.992467
4        4 4           164647  163125.8       1521  0.9325628   4.992467
# ℹ 3 more variables: Shape_Leng <dbl>, Shape_Area <dbl>,
#   geometry <MULTIPOLYGON [m]>

Geospatial Data Formats: GeoJSON

Geospatial Data Formats: GeoJSON

read_sf("data/Portland_City_Council_Districts.geojson")
Simple feature collection with 4 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -122.8368 ymin: 45.43254 xmax: -122.472 ymax: 45.65289
Geodetic CRS:  WGS 84
# A tibble: 4 × 10
  OBJECTID DISTRICT pop_total Ideal_pop Diff_Ideal DDeviation ODeviation
     <int> <chr>        <int>     <dbl>      <int>      <dbl>      <dbl>
1        1 1           159498  163125.8      -3628 -2.223898    4.992467
2        2 2           160716  163125.8      -2410 -1.477235    4.992467
3        3 3           167642  163125.8       4516  2.768570    4.992467
4        4 4           164647  163125.8       1521  0.9325628   4.992467
# ℹ 3 more variables: Shape_Length <dbl>, Shape_Area <dbl>,
#   geometry <MULTIPOLYGON [°]>

Geospatial Data Formats: Others

TODO: center slide

Geospatial Data Formats: Others

  • Almost all file formats can be imported with read_sf()

Your Turn

  • Download data (ESRI Shapefile or GeoJSON) on Portland sextants

  • Import the data in R using the read_sf() function from the sf package and save it as portland_sextants

  • Examine portland_sextants, making sure you can identify its geometry type, dimensions, bounding box, coordinate reference system and geometry column

Learn More

https://r-spatial.github.io/sf/articles/sf2.html

Turning Data Frames into sf Objects

st_as_sf()

library(sf)

csv_file |>
  st_as_sf(
    coords = c("x_variable", "y_variable"),
    crs = 4326
  )
portland_corners_csv <-
  read_csv("data/portland-corners.csv")
portland_corners_csv
# A tibble: 38,492 × 2
   longitude latitude
       <dbl>    <dbl>
 1 -122.6607 45.60120
 2 -122.6606 45.60167
 3 -122.6605 45.60116
 4 -122.6604 45.60164
 5 -122.6603 45.60056
 6 -122.6603 45.60072
 7 -122.6602 45.60079
 8 -122.6551 45.59178
 9 -122.6546 45.59212
10 -122.6538 45.59125
# ℹ 38,482 more rows
portland_corners_sf <-
  portland_corners_csv |> 
  st_as_sf(
    coords = c("longitude", "latitude"),
    crs = 4326
  )
portland_corners_sf
Simple feature collection with 38492 features and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -122.8301 ymin: 45.43283 xmax: -122.4619 ymax: 45.63909
Geodetic CRS:  WGS 84
# A tibble: 38,492 × 1
               geometry
 *          <POINT [°]>
 1  (-122.6607 45.6012)
 2 (-122.6606 45.60167)
 3 (-122.6605 45.60116)
 4 (-122.6604 45.60164)
 5 (-122.6603 45.60056)
 6 (-122.6603 45.60072)
 7 (-122.6602 45.60079)
 8 (-122.6551 45.59178)
 9 (-122.6546 45.59212)
10 (-122.6538 45.59125)
# ℹ 38,482 more rows
portland_corners_sf |>
  mapview::mapview()

Your Turn

  1. Download this CSV of traffic signal data for the city of Portland

  2. Import the CSV and turn it into an sf object using st_as_sf()

  3. Run mapview::mapview() to ensure you imported it correctly